package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IApUserSearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    @Async
    public void saveSearchRecord(String keyword, Integer userId, String equipmentId) {
        // 判断当前用户是否已经保存了这个关键词的搜索
        Query query = new Query();
        if (userId != null && userId != 0) {
            query.addCriteria(Criteria.where("userId").is(userId));
        } else {
            query.addCriteria(Criteria.where("equipmentId").is(equipmentId));
        }
        query.addCriteria(Criteria.where("keyword").is(keyword));
        ApUserSearch one = mongoTemplate.findOne(query, ApUserSearch.class);
        // 保存用户的搜索记录
        if (one == null) {
            ApUserSearch userSearch = new ApUserSearch();
            if (userId != null && userId != 0) {
                userSearch.setUserId(userId);
            } else {
                userSearch.setEquipmentId(equipmentId);
            }
            userSearch.setKeyword(keyword);
            userSearch.setCreatedTime(new Date());
            mongoTemplate.save(userSearch);
        }
    }

    /**
     * 加载搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        User user = AppThreadLocalUtil.get();
        Query query=new Query();
        if(user!=null){
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        }else{
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }
        Pageable page= PageRequest.of(0,5, Sort.Direction.DESC,"createdTime");
        query.with(page);
        List<ApUserSearch> searches = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(searches);
    }


    /**
     * 删除搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult delete(ApArticleSearchDto dto) {
        List<ApUserSearch> hisList = dto.getHisList();
        List<String> list=new ArrayList<>();
         for (ApUserSearch apUserSearch : hisList) {
            list.add(apUserSearch.getId());
        }
        Query query=new Query();
        query.addCriteria(Criteria.where("id").in(list));
        mongoTemplate.remove(query,ApUserSearch.class);
        return ResponseResult.okResult();
    }


}
